package com.sohu.pickoo.frag;

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

import com.sohu.peak.util.ConfigManager;
import com.sohu.peak.util.Logger;


public class FragServer {

	public void run() throws Exception {
        Logger log = new Logger();

        log.debug("------- Initializing -------------------");

        // First we must get a reference to a scheduler
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        log.debug("------- Initialization Complete --------");
        
        log.debug("------- Connecting Database ----------------");
        
        java.sql.Connection   lConn   =   null;   
        java.sql.Statement   lStat   =   null;   
        java.sql.ResultSet   lRs   =   null;   

        Class driverClass;
		driverClass = Class.forName(ConfigManager.instance().getProperty(
				"frag", "pickooDB.dirver"));
		DriverManager.registerDriver((Driver) driverClass.newInstance());
		lConn = DriverManager.getConnection(ConfigManager.instance()
				.getProperty("frag", "pickooDB.url"), ConfigManager
				.instance().getProperty("frag", "pickooDB.user"),
				ConfigManager.instance().getProperty("frag",
						"pickooDB.passwd"));   
        lStat   =   lConn.createStatement();   

        log.debug("------- Connection Complete --------");
        
        log.debug("------- Getting Jobs From Database----------------");
        
        ResultSet rs = lStat.executeQuery("select * from FRAG where is_stop=0");
        
        log.debug("------- Get Jobs Complete----------------");
        
        log.debug("------- Scheduling Jobs ----------------");
        //jobs can be scheduled before sched.start() has been called
        if (rs!=null){
        	while (rs.next()){
        		log.debug("------- Job "+rs.getString("id")+"----------------");
        		JobDetail job = new JobDetail("job_"+rs.getString("id"), "group1", FragJob.class);
        		JobDataMap dataMap = new JobDataMap();
        		dataMap.put("source",rs.getString("source"));
        		dataMap.put("target",rs.getString("target"));
        		job.setJobDataMap(dataMap);
                CronTrigger trigger = new CronTrigger("trigger_"+rs.getString("id"), "group1", "job_"+rs.getString("id"),
                        "group1", rs.getString("timer"));
                sched.addJob(job, true);
                Date ft = sched.scheduleJob(trigger);
                log.debug(job.getFullName() + " has been scheduled to run at: " + ft
                        + " and repeat based on expression: "
                        + trigger.getCronExpression());
        	}
        	
        }
 
        log.debug("------- Starting Scheduler ----------------");

        // All of the jobs have been added to the scheduler, but none of the
        // jobs
        // will run until the scheduler has been started
        sched.start();

        log.debug("------- Started Scheduler -----------------");

        log.debug("------- Waiting For Executing... ------------");
        try {
        	while (true){
        		// wait five minutes for executing
        		Thread.sleep(300L * 1000L);
        		// executing...
        	}
        } catch (Exception e) {
        }

    }

    public static void main(String[] args) throws Exception {

    	FragServer server = new FragServer();
    	server.run();
    }
}
